<?xml version="1.0"?>
<rss version="2.0">
  <channel><title>AJAX Tools</title><link>http://bill.welliver.org//space/pike/Fins/AJAX Tools</link><description>AJAX (Asyncronous Javascript and XML) seems to be the latest craze in the online development world. Everyone's trying to incorporate it into their web applications. Luckily, the result is, in general, better than what we got when the "scroll" tag was in vogue, and with help from Fins, almost as easy to produce. The catch is that except for very simple applications, you're going to have to learn a little DOM and some JavaScript, but it's totally manageable.&lt;p class="paragraph"/&gt;
&lt;b class="bold"&gt;Dojo: a cross browser application toolkit&lt;/b&gt;&lt;p class="paragraph"/&gt;
We've decided to recommend the use of the wonderful &lt;a href="Dojo" class="wiki_link_external" &gt;http://www.dojotoolkit.org&lt;/a&gt; toolkit for your AJAX enabled Fins applications. Its proficiencies go far beyond just AJAX, and includes features such as client side implimentations of crypto algorithms and client side persistent storage. It's a little complicated, mostly due to the vast scope of what they're trying to accomplish, but after a little bit of time, you'll be getting your Dojo Mojo on (sorry, couldn't resist!) At the moment, we're not bundling Dojo, but our AJAX support macros assume a copy of Dojo installed in your app's &lt;i class="ital"&gt;/static/javascripts&lt;/i&gt; directory.&lt;p class="paragraph"/&gt;
&lt;b class="bold"&gt;AJAX Support Macros&lt;/b&gt;&lt;p class="paragraph"/&gt;
Fins includes some macros that make it easier to perform some simple AJAX operations. Things like asyncronous form submission, and asyncronous link actions. Details of how to use these macros are available on the modref for &#xD;
&lt;a href="AJAX Macros" class="wiki_link_external" &gt;http://hww3.riverweb.com/dist/Fins/modref/ex/predef_3A_3A/Fins/Helpers/Macros/JavaScript.html&lt;/a&gt;. See the SmugMug sample app for a quick example of how to use the AJAX macros.&lt;p class="paragraph"/&gt;
&lt;b class="bold"&gt;JSON and JSON-RPC: JavaScript friendly data interchange&lt;/b&gt;&lt;p class="paragraph"/&gt;
&lt;a href="JSON" class="wiki_link_external" &gt;http://www.json.org&lt;/a&gt; is a lightweight data interchange format optimized for use with JavaScript applications. You can easily encode your native Pike data to JSON and send it to a waiting browser for use more quickly and easily than other formats such as XML. Additionally, JSON-RPC allows you to make cross-application method calls that are easier to produce  than SOAP or even XMLRPC.&lt;p class="paragraph"/&gt;
Fins includes a module, called Tools.JSON, that allows you to serialize native Pike data to a JSON format string, and conversely deserialize a JSON string into native Pike datatypes. These methods allow you to easily send and receive data in a format that JavaScript enabled browsers can easily digest.&lt;p class="paragraph"/&gt;
Additionally, Fins includes some tools for generating and decoding JSON-RPC calls, both in Tools.JSON and in Fins.JSONRPCController,  which will allow you to create remotely callable functions quickly and easily.&lt;p class="paragraph"/&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;pre&gt;&#xD;
&amp;gt; &lt;b&gt;&lt;font color=darkgreen&gt;mapping &lt;/font&gt;&lt;/b&gt;&lt;b&gt;&lt;font color=darkbrown&gt;m&lt;/font&gt;&lt;/b&gt; = (&amp;#91;&lt;i&gt;&lt;font color=darkred&gt;"foo"&lt;/font&gt;&lt;/i&gt;: 1, &lt;i&gt;&lt;font color=darkred&gt;"bar"&lt;/font&gt;&lt;/i&gt;: &lt;i&gt;&lt;font color=darkred&gt;"gazonk"&lt;/font&gt;&lt;/i&gt;]);&#xD;
&amp;gt; &lt;b&gt;&lt;font color=darkgreen&gt;string &lt;/font&gt;&lt;/b&gt;&lt;b&gt;&lt;font color=darkbrown&gt;s&lt;/font&gt;&lt;/b&gt; = Tools.JSON.serialize(m);&#xD;
&amp;gt; s;&#xD;
(1) Result: &lt;i&gt;&lt;font color=darkred&gt;"{&amp;amp;#34;bar&amp;amp;#34;:&amp;amp;#34;gazonk&amp;amp;#34;,&amp;amp;#34;foo&amp;amp;#34;:1}"&lt;/font&gt;&lt;/i&gt;&#xD;
&amp;gt; Tools.JSON.deserialize(s);&#xD;
(2) Result: (&amp;#91; &lt;font color=red&gt;/* 2 elements */&lt;/font&gt;&#xD;
              &lt;i&gt;&lt;font color=darkred&gt;"bar"&lt;/font&gt;&lt;/i&gt;: &lt;i&gt;&lt;font color=darkred&gt;"gazonk"&lt;/font&gt;&lt;/i&gt;,&#xD;
              &lt;i&gt;&lt;font color=darkred&gt;"foo"&lt;/font&gt;&lt;/i&gt;: 1&#xD;
            ])&#xD;
&lt;/pre&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p class="paragraph"/&gt;
&#xD;
&lt;b class="bold"&gt;Gracefully degrading&lt;/b&gt;&lt;p class="paragraph"/&gt;
Even though it's been around for years, in many ways AJAX is cutting edge technology. Not all browsers support all of the functionality needed to create AJAX apps, and even among those that do, there is a great deal of inconsistency. Dojo goes a long way toward smoothing over these rough edges. Sometimes, however, you'll run across a browser that doesn't support dojo. Perhaps, even, you want to make your AJAX functionality optional. The question then becomes, how do you provide the rich functionality and still provide a usable interface when AJAX isn't being used?&lt;p class="paragraph"/&gt;
The first thing we need to be able to do is detect whether our toolkit (Dojo), has been loaded. To do this, we need some tool to tell us whether a given identifier is present. The following snippet of code will return true when a given identifier is undefined.&lt;p class="paragraph"/&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;pre&gt;&#xD;
  var app_global = this;&lt;p class="paragraph"/&gt;
  &lt;font color=red&gt;// stolen shamelessly from dojo&#xD;
&lt;/font&gt;  &lt;b&gt;&lt;font color=darkgreen&gt;function &lt;/font&gt;&lt;/b&gt;&lt;b&gt;&lt;font color=darkbrown&gt;app_undef&lt;/font&gt;&lt;/b&gt;(identifier)&#xD;
  {&#xD;
     if(!app_global) app_global = window;&lt;p class="paragraph"/&gt;
     &lt;b&gt;&lt;font color=darkblue&gt;return&lt;/font&gt;&lt;/b&gt; (typeof app_global&amp;#91;identifier] == &lt;i&gt;&lt;font color=darkred&gt;"undefined"&lt;/font&gt;&lt;/i&gt;);&#xD;
  }&lt;p class="paragraph"/&gt;
&lt;/pre&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p class="paragraph"/&gt;
&#xD;
We could use it in the following way:&lt;p class="paragraph"/&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;pre&gt;&#xD;
  &lt;b&gt;&lt;font color=darkblue&gt;if&lt;/font&gt;&lt;/b&gt;(app_undef(&lt;i&gt;&lt;font color=darkred&gt;"dojo"&lt;/font&gt;&lt;/i&gt;))&#xD;
    alert(&lt;i&gt;&lt;font color=darkred&gt;"Dojo isn't available!"&lt;/font&gt;&lt;/i&gt;);&#xD;
&lt;/pre&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p class="paragraph"/&gt;
&#xD;
Obviously, this is a simple example that doesn't get us very far. In a real world application, we'd most likely use code like this to fire off a bootstrap method from our document's onLoad event. By default, we'd write the application to use non-AJAX functionality. The bootstrap method would set up event handlers such as "onclick" and correct any URLs that would need to change when we run in AJAX mode. Using this technique gives us seamless switchover between AJAX and "old school" applications.&lt;p class="paragraph"/&gt;
Good examples of these techniques can be found in the FinScribe application; look in &lt;i class="ital"&gt;static/javascripts/bootstrap.js&lt;/i&gt;, ~~templates/header.phtml~~ and &lt;i class="ital"&gt;templates/bottom.phtml&lt;/i&gt;.&lt;p class="paragraph"/&gt;
&lt;b class="bold"&gt;Useful Resources&lt;/b&gt;&#xD;
&lt;ul class="minus"&gt;
&lt;li&gt;http://ajaxload.info - A site that allows you to generate custom "spinners"&lt;/li&gt;
&lt;/ul&gt;</description><generator>Fins 0.9.7</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs></channel>
</rss>
